const http = require('http')
const fs = require('fs')
const exec = require('child_process').exec;

const server = http.createServer(async (req, res) => {
  let temperatureFile = "/sys/class/thermal/thermal_zone0/temp"
  let temperatureCmd = "/opt/vc/bin/vcgencmd measure_temp"
  let content = ""
  try {
    content = fs.readFileSync(temperatureFile).toString()
  }
  catch(e) {
    // try {
    //   content = exec(temperatureCmd)
    // }
    // catch(e) {
      content = "no temperature"
    // }
  }
  res.setHeader("Access-Control-Allow-Origin", "*");
  res.writeHead(200, { 'Content-Type': 'application/json' });
  res.end(content);
});

server.listen(8888, () => {})